package com.victor.controller;

import static org.hamcrest.CoreMatchers.nullValue;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.record.MMSRecord;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.Infomation;
import com.victor.entity.InfomationArticle;
import com.victor.entity.InfomationPhoto;
import com.victor.service.InfomationService;
import com.victor.utils.Msg;
import com.victor.utils.OSSUtil;

@RestController
@RequestMapping("infomation")
public class InfomationController {
	@Autowired
	private InfomationService infomationService;
	@Autowired
	private LogController logcontroller;

	/**
	 * 添加信息流
	 * 
	 * @param infomation
	 * @return
	 */
	@RequestMapping("/addinfo")
	public Msg addInfomation(Infomation infomation, InfomationArticle infomationArticle,
			InfomationPhoto infomationPhoto,String adminid) {
		if (infomation == null || adminid==null|| adminid.isEmpty() || infomation.getFlag()==null || infomation.getFlag().isEmpty() ) {
			return Msg.fail("入参错误");
		}
		boolean b = infomationService.insert(infomation);
		if (!b) {
			return Msg.fail("插入信息流错误");
		}
		if (infomationArticle != null) {
			infomationArticle.setInfoid(String.valueOf(infomation.getId()));
			boolean b1 = infomationService.inserArticle(infomationArticle);
			if (!b1) {
				return Msg.fail("插入文案失败");
			}
		}
		if (infomationPhoto != null) {
			infomationPhoto.setInfoid(String.valueOf(infomation.getId()));
			boolean b2 = infomationService.insertPhoto(infomationPhoto);
			if (!b2) {
				return Msg.fail("添加图片失败");
			}
		}
		logcontroller.addLog(adminid, "添加了信息流");
		return Msg.success();

	}

	/**
	 * 添加文案
	 * 
	 * @param infomationArticle
	 * @return
	 */
	@RequestMapping("/addarticle")
	public Msg addAticle(InfomationArticle infomationArticle,String adminid) {
		if (infomationArticle == null || infomationArticle.getInfoid().isEmpty()|| adminid== null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean article = infomationService.inserArticle(infomationArticle);
		logcontroller.addLog(adminid, "添加了文案");
		return Msg.judge(article);

	}

	/**
	 * 添加图片
	 * 
	 * @param infomationPhoto
	 * @return
	 */
	@RequestMapping("/addphoto")
	public Msg addPhoto(@RequestBody List<InfomationPhoto> infomationPhotos) {
		if (infomationPhotos == null || infomationPhotos.size()<=0 || infomationPhotos.get(0).getInfoid() == null || infomationPhotos.get(0).getInfoid().isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean insertPhoto=false;
		for (int i = 0; i < infomationPhotos.size(); i++) {
			insertPhoto = infomationService.insertPhoto(infomationPhotos.get(i));
		}
		logcontroller.addLog(infomationPhotos.get(0).getAdminid(), "添加了信息流"+infomationPhotos.get(0).getInfoid()+"的图片");
		return Msg.judge(insertPhoto);
	}

	/**
	 * 修改信息流
	 * 
	 * @param infomation
	 * @return
	 */
	@RequestMapping("/updateinfo")
	public Msg updateInfomation(Infomation infomation,String adminid) {
		if (infomation == null || infomation.getId() == null || adminid== null || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean updateInfomation = infomationService.updateInfomation(infomation);
		logcontroller.addLog(adminid, "修改了"+infomation.getId()+"的信息流");
		return Msg.judge(updateInfomation);
	}

	/**
	 * 修改文案
	 * 
	 * @param infomationArticle
	 * @return
	 */
	@RequestMapping("/updatearticle")
	public Msg updateArticle(InfomationArticle infomationArticle,String adminid) {
		if (infomationArticle == null || infomationArticle.getAid() == null || adminid== null  || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean article = infomationService.updateArticle(infomationArticle);
		logcontroller.addLog(adminid, "修改了"+infomationArticle.getInfoid()+"的文案");
		return Msg.judge(article);

	}

	/**
	 * 修改图片
	 * 
	 * @param infomationPhoto
	 * @return
	 */
	@RequestMapping("/updatephoto")
	public Msg updatePhoto(InfomationPhoto infomationPhoto,String adminid) {
		if (infomationPhoto == null || infomationPhoto.getTid() == null || adminid== null  || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		InfomationPhoto photo2 = infomationService.selPhotoById(infomationPhoto.getTid());
		OSSUtil.deleteFile(photo2.getPhotopath());
		boolean photo = infomationService.updatePhoto(infomationPhoto);
		logcontroller.addLog(adminid, "修改了"+infomationPhoto.getInfoid()+"的图片");
		return Msg.judge(photo);

	}

	/**
	 * 删除信息流
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delinfo")
	public Msg deleteInfo(int[] ids,String adminid) {
		if (ids == null || ids.length <= 0 || adminid.isEmpty() || adminid== null  || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		for (int i = 0; i < ids.length; i++) {
			List<InfomationPhoto> infomationPhoto = infomationService.selPhotoByInfoId(ids[i]);
			for (int j = 0; j < infomationPhoto.size(); j++) {
				OSSUtil.deleteFile(infomationPhoto.get(j).getPhotopath());
			}
			
		}
		boolean infomation = infomationService.delInfomation(ids);
		logcontroller.addLog(adminid, "删除了信息流"+ids);
		return Msg.judge(infomation);
	}

	/**
	 * 删除文案
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delarticle")
	public Msg delArticle(int[] ids,String adminid) {
		if (ids == null || ids.length <= 0 || adminid.isEmpty() || adminid== null  || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean article = infomationService.delArticle(ids);
		logcontroller.addLog(adminid, "删除了文案"+ids);
		return Msg.judge(article);

	}
	/**
	 * 删除图片
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delPhoto")
	public Msg delPhoto(int[] ids,String adminid) {
		if (ids == null || ids.length <= 0 || adminid.isEmpty() || adminid== null  || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		for (int i = 0; i < ids.length; i++) {
			InfomationPhoto infomationPhoto = infomationService.selPhotoById(ids[i]);
			OSSUtil.deleteFile(infomationPhoto.getPhotopath());
		}
		boolean photo = infomationService.delPhoto(ids);
		logcontroller.addLog(adminid, "删除了图片"+ids);
		return Msg.judge(photo);
	}
	/**
	 * 信息流列表
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/list")
	public Msg findAll(int page,int row,String adminid) {
		if (adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<Infomation> pageInfo = infomationService.selAll(page,row);
		logcontroller.addLog(adminid, "查看了信息流列表");
		return Msg.success().add("list", pageInfo);
	}
	/**
	 * 导出信息流
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/export")
	public void export(HttpServletRequest request,HttpServletResponse response,String adminid) throws Exception {
		List<Infomation> list = infomationService.selAllNoPage();
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		Sheet sheet = sxssfWorkbook.createSheet("信息流列表");
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short)0, (short)5);
		sheet.addMergedRegion(cellRangeAddress);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("信息流列表");
		
		Row headrow = sheet.createRow(4);
		headrow.createCell(0).setCellValue("标题");
		headrow.createCell(1).setCellValue("省");
		headrow.createCell(2).setCellValue("市");
		headrow.createCell(3).setCellValue("时间");
		headrow.createCell(4).setCellValue("文案");
		headrow.createCell(5).setCellValue("图片");
		
		for(Infomation infomation : list) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum()+1);
			if(infomation.getTitle()== null) {
				dataRow.createCell(0).setCellValue("");
			}else {
				dataRow.createCell(0).setCellValue(infomation.getTitle());
			}
			
			if(infomation.getProvince() == null) {
				dataRow.createCell(1).setCellValue("");
			}else {
				dataRow.createCell(1).setCellValue(infomation.getProvince());
			}
			if(infomation.getCity() == null) {
				dataRow.createCell(2).setCellValue("");
			}else {
				dataRow.createCell(2).setCellValue(infomation.getCity());
			}
			if(infomation.getTime()== null) {
				dataRow.createCell(3).setCellValue("");
			}else {
				dataRow.createCell(3).setCellValue(infomation.getTime());
			}
			if(infomation.getArticliList() == null) {
				dataRow.createCell(4).setCellValue("");
			}else {
				for (int i = 0; i < infomation.getArticliList().size(); i++) {
					dataRow.createCell(4).setCellValue(infomation.getArticliList().get(i).getContent());
					if (infomation.getPhotoList()!=null) {
						dataRow= sheet.createRow(sheet.getLastRowNum()+1);
					}
				}
			}
			if(infomation.getPhotoList() == null) {
				dataRow.createCell(5).setCellValue("");
			}else {
				for (int i = 0; i < infomation.getPhotoList().size(); i++) {
					dataRow.createCell(5).setCellValue(infomation.getPhotoList().get(i).getPhotopath());
					dataRow= sheet.createRow(sheet.getLastRowNum()+1);
				}
			}
		}
		String filename = "信息流列表";
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		ServletOutputStream outputStream = response.getOutputStream();
		sxssfWorkbook.write(outputStream);
		outputStream.close();
		sxssfWorkbook.close();
		logcontroller.addLog(adminid, "导出了信息流");
	}
	/**
	 * 信息流详情
	 * @param infomation
	 * @return
	 */
	@RequestMapping("/xiangqing")
	public Msg getXiangqing(Infomation infomation,String adminid) {
		if (infomation == null || adminid== null   || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		infomation = infomationService.selInfomationXiangqing(infomation);
		logcontroller.addLog(adminid, "查看了信息流"+infomation.getId()+"详情");
		return Msg.success().add("infomation", infomation);
	}
	/**
	 * 搜索
	 * @param search
	 * @return
	 */
	@RequestMapping("/search")
	public Msg search(String search,int page,int row,String adminid) {
		if (search == null || search.isEmpty() || adminid== null  || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<Infomation> list = infomationService.selBySearch(search,page,row);
		logcontroller.addLog(adminid, "根据"+search+"搜索了信息流");
		return Msg.success().add("list", list);
	}
	/**
	 * 时间搜索
	 * @param start
	 * @param end
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/timesearch")
	public Msg searchTime(String start,String end,int page,int row,String adminid) {
		if (start== null && end == null || adminid== null  || adminid== null  || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		PageInfo<Infomation> list = infomationService.selBytime(start,end,page,row);
		logcontroller.addLog(adminid, "根据时间搜索了信息流");
		return Msg.success().add("list", list);
		
	}

}
